﻿using System.Collections.Generic;
using SlamCms.Common;
using SlamCms.Data;

namespace SlamCms.Web.Sitemaps
{
	public class SitemapBuilder
	{
		private readonly DataContext _dataContext;

		internal SitemapBuilder(DataContext dataContext, IEnumerable<DynamicSitemapNode> initialNodes = null)
		{
			_dataContext = dataContext;
			var nodes = new List<DynamicSitemapNode>();

			if (initialNodes != null)
				nodes.AddRange(initialNodes);

			Nodes = nodes;
		}

		public DataContext DataContext
		{
			get
			{
				return _dataContext;
			}
		}

		public IList<DynamicSitemapNode> Nodes { get; private set; }

		public DynamicSitemapNode AddNode(string key, string title, string url)
		{
			return AddNode(string.Empty, key, title, url);
		}

		public DynamicSitemapNode AddNode(string parentKey, string key, string title, string url, int order = 1)
		{
			Ensure.ArgumentNotNullOrEmpty(key, "key");
			Ensure.ArgumentNotNull(title, "title");
			
			var dynamicNode = new DynamicSitemapNode()
			{
				Key = key,
				ParentKey = parentKey,
				Title = title,
				Url = url,
				Order = order
			};

			Nodes.Add(dynamicNode);

			return dynamicNode;
		}
	}
}
